package tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpUtil {



    private OnNeedSendMessageInterface mOnNeedSendMessageInterface;

    public OkHttpUtil(OnNeedSendMessageInterface mOnNeedSendMessageInterface) {
        this.mOnNeedSendMessageInterface = mOnNeedSendMessageInterface;
    }

    public void setOnNeedSendMessageInterface(OnNeedSendMessageInterface mOnNeedSendMessageInterface) {
        this.mOnNeedSendMessageInterface = mOnNeedSendMessageInterface;
    }


    //获取url中网页源码
    public void getOkHttpString(String url) {

        OkHttpClient okHttpClient = null;
        Request request = null;
        Response response = null;
        Call call = null;
        try {
            okHttpClient = new OkHttpClient();
            request = new Request.Builder()
                    .url(url)
                    .build();

//            response = okHttpClient.newCall(request).execute();

            call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    mOnNeedSendMessageInterface.OnNeedSendMessageForFAll();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String strHTML = response.body().string();
                    mOnNeedSendMessageInterface.OnNeedSendMessageForStr(strHTML);

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     *  
     *      * 获取网落图片资源  
     *      * @param url 
     *      * @return 
     *      
     */
    public void getOkHttpBitmap(String url) {
        final Bitmap bitmap = null;
        OkHttpClient okHttpClient = null;
        Request request = null;
        Response response = null;
        Call call = null;
        try {
            okHttpClient = new OkHttpClient();
            request = new Request.Builder()
                    .url(url)
                    .build();

//            response = okHttpClient.newCall(request).execute();

            call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    mOnNeedSendMessageInterface.OnNeedSendMessageForFAll();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    byte[] Picture_bt = response.body().bytes();
                    mOnNeedSendMessageInterface.OnNeedSendMessageForBytes(Picture_bt);
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public interface OnNeedSendMessageInterface{
        void OnNeedSendMessageForStr(String str);
        void OnNeedSendMessageForBytes(byte[] bytes);//返回Bytes数组
        void OnNeedSendMessageForFAll();
    }


}
